package com.example.demo0



class PartialFunction<in P1,out R>(
    private val definetAt:(P1)->Boolean,
                                   private val f: (P1)->R
):
        (P1)->R
{



    override fun invoke(p1: P1): R {
        if(definetAt(p1)){
            return f(p1)
        }
        else{
            throw IllegalArgumentException("Value:($p1) isn't supported by this function")
        }
    }

    fun isDefinedAt(p1: P1) = definetAt(p1)

}

infix fun <P1,R> PartialFunction<P1,R>.orElse(that:PartialFunction<P1,R>):PartialFunction<P1,R>{
    return PartialFunction({
        this.isDefinedAt(it)||this.isDefinedAt(it)
    }){
        when{
            this.isDefinedAt(it)->this(it)
            else->that(it)
        }
    }
}

























